<?php
namespace bee;

define("MATCH_TYPE_BEGINS",   1);
define("MATCH_TYPE_CONTAINS", 2);

class textHelper extends object
{
  private   $data;
  public    $matchType;
  private   $filter;
  protected $DSDoc;
  public $id;
  public function __construct(&$source, $colName = 0, $id="__textHelper")
  { $this->__withEvents = true;
    $this->DSDoc = XMLDataHub::getInstance();//DOMSQLDocument::getInstance();
    $this->id = $id;
    dispatcher::getInstance()->addObject($this);
   
    if(is_array($source))
    { $this->data = $source;
    } elseif(is_a($source, "bee\DAO")) 
    { $rs = new rowSet($source);
      $this->data = $rs->getList($colName);
    }
    //var_dump($this->data);
    $this->matchType = MATCH_TYPE_CONTAINS;
  }
  
  function __wakeup()
  { $this->DSDoc = XMLDataHub::getInstance();
    dispatcher::getInstance()->addObject($this);
  }
  
  public function filter_data($filter)
  { $this->filter = isset($filter) ? $filter : ""; 
    return array_filter($this->data, array($this, "callback_filter"));
  }
  
  private function callback_filter($val)
  { //echo "<br />$val / $this->filter / " . stripos($val, $this->filter);
    if(stripos($val, $this->filter) === false)
    { return false;
    } else
    { return true;
    } 
  }
  
  public function drawMe($evt)
  { //echo print_r($evt);
  
    if(isset($evt["__filter"]))
    { //$ar = $this->filter_data($evt["__filter"]);
      //var_dump($ar); 
      $this->DSDoc->setArray($this->filter_data($evt["__filter"]), "helper");
    } else 
    { $this->DSDoc->setArray($this->data, "xelper");
      //var_dump($this->data);      
    }
    //var_dump($this->DSDoc);
    
  }
  
}
